<template>
  <kinglist
    :goodList="goodList"
    @delgood="delgood"
    @addloogsum="addloogsum"
    @delgoodsun="delgoodsun"
    @change="changeFn"
   @changeAll="changeAllFn"
  />
</template>

<script>
import kinglist from './components/01kinglist.vue'
export default {
  components: {
    kinglist,
  },
  data() {
    return {
      goodList: [
        {
          name: '诸葛亮',
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: '蔡文姬',
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: '妲己',
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: '鲁班',
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    }
  },
 
  methods: {
    // 删除
    delgood(name) {
      // console.log(name);
      this.goodList = this.goodList.filter((item) => item.name !== name)
    },
    // 增加数量
    addloogsum(name) {
      // console.log(name);
      this.goodList.forEach((item) => {
        if (item.name === name) {
          item.num++
        }
      })
    },
    // 减除数量
    delgoodsun(name) {
      // console.log(name);
     
        this.goodList.forEach((item) => {
        if (item.name === name && item.num > 0) {
          item.num--
        }
      })
      
    },
    changeFn(name) {
      // console.log(name);
      const result = this.goodList.find((item) => item.name === name)
      result.checked = !result.checked
    },
     changeAllFn(val) {
      console.log(val)
      this.goodList.forEach((v) => (v.checked = val))
      
    },
  },
  

}
</script>

<style></style>
